package info.androidhive.slidingmenu;

import info.androidhive.slidingmenu.MusicService.MusicBinder;
import info.androidhive.slidingmenu.adapter.SongsAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.TextView;

public class SongsActivity extends Activity implements MediaPlayerControl {
	private ArrayList<Songs> songsList;
	private ListView songsView;
	private MusicService musicSrv;
	private Intent playIntent;
	private boolean musicBound = false;
	private boolean paused = false;
	
	TextView songName;
	TextView artistName;
	ImageButton imgPrev;
	ImageButton imgPlay;
	ImageButton imgNext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_songs);

		songsView = (ListView) findViewById(R.id.listSongs);
		songsList = new ArrayList<Songs>();

		getSongsList();

		Collections.sort(songsList, new Comparator<Songs>() {
			@Override
			public int compare(Songs lhs, Songs rhs) {
				return lhs.getTitle().compareTo(rhs.getTitle());
			}
		});

		SongsAdapter songAdt = new SongsAdapter(this, songsList);
		songsView.setAdapter(songAdt);

		setController();
	}

/*	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_shuffle:
			musicSrv.setShuffle();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}*/

	/*
	 * Connect to the service
	 */
	private ServiceConnection musicConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			musicBound = false;

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			MusicBinder binder = (MusicBinder) service;

			// Get service
			musicSrv = binder.getService();

			// Pass list
			musicSrv.setList(songsList);

			musicBound = true;

		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		if (playIntent == null) {
			playIntent = new Intent(this, MusicService.class);
			bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
			startService(playIntent);
		}
	}

	public void getSongsList() {
		// retrieve song info
		ContentResolver musicResolver = getContentResolver();
		Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		Cursor musicCursor = musicResolver.query(musicUri, null, null, null,
				null);

		if (musicCursor != null && musicCursor.moveToFirst()) {
			// get columns
			int titleColumn = musicCursor
					.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
			int idColumn = musicCursor
					.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
			int artistColumn = musicCursor
					.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
			// add songs to list
			do {
				long thisId = musicCursor.getLong(idColumn);
				String thisTitle = musicCursor.getString(titleColumn);
				String thisArtist = musicCursor.getString(artistColumn);
				songsList.add(new Songs(thisId, thisTitle, thisArtist));
			} while (musicCursor.moveToNext());
		}
	}

	/*
	 * Handle event click song item
	 */
	public void songPicked(View view) {
		musicSrv.setSong(Integer.parseInt(view.getTag().toString()));
		musicSrv.playSong();
		
		setController();
	}

	@Override
	protected void onPause() {

		super.onPause();
		paused = true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (paused) {
			setController();
			paused = false;
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		stopService(playIntent);
		musicSrv = null;
		super.onDestroy();
	}

	private void setController() {

		songName = (TextView) findViewById(R.id.txtSongName);
		artistName = (TextView) findViewById(R.id.txtArtistName);
		imgPrev = (ImageButton) findViewById(R.id.imgPrev);
		imgPlay = (ImageButton) findViewById(R.id.imgPlayPause);
		imgNext = (ImageButton) findViewById(R.id.imgNext);

		songName.setText(songsList.get(getCurrentPosition()).getTitle());
		artistName.setText(songsList.get(getCurrentPosition()).getArtist());
		imgPrev.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				playPrev();

			}
		});

		imgNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				playNext();

			}
		});
		
		imgPlay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (isPlaying()){
					pause();
					imgPlay.setBackgroundResource(android.R.style.MediaButton_Play);
					
				} else {
					start();
					imgPlay.setBackgroundResource(android.R.style.MediaButton_Pause);
				}
				
			}
		});

	}

	private void playNext() {
		musicSrv.playNext();
		songName.setText(songsList.get(getCurrentPosition()).getTitle());
		artistName.setText(songsList.get(getCurrentPosition()).getArtist());

	}

	private void playPrev() {
		musicSrv.playPrev();
		songName.setText(songsList.get(getCurrentPosition()).getTitle());
		artistName.setText(songsList.get(getCurrentPosition()).getArtist());

	}
	
	public void onClickController(View view) {
		
		Intent intent = new Intent(SongsActivity.this, PlayingActivity.class);
		intent.putExtra("SongsList", songsList);
		intent.setClass(this, PlayingActivity.class);
		startActivity(intent);
	}
	

	@Override
	public void start() {
		musicSrv.go();
		setController();

	}

	@Override
	public void pause() {
		musicSrv.pausePlayer();
		setController();
	}

	@Override
	public int getDuration() {
		if (musicSrv != null && musicBound)
			return musicSrv.getDur();
		// else
		return 0;
	}

	@Override
	public int getCurrentPosition() {
		if (musicSrv != null && musicBound)
		{
			return musicSrv.getPosn();
		}
		// else
		return 0;
	}

	@Override
	public void seekTo(int pos) {
		musicSrv.seek(pos);

	}

	@Override
	public boolean isPlaying() {
		if (musicSrv != null && musicBound)
			return musicSrv.isPng();
		// else
		return false;
	}

	@Override
	public int getBufferPercentage() {
		return 0;
	}

	@Override
	public boolean canPause() {

		return true;
	}

	@Override
	public boolean canSeekBackward() {

		return true;
	}

	@Override
	public boolean canSeekForward() {

		return true;
	}

	@Override
	public int getAudioSessionId() {
		return 0;
	}
}
